//package com.geely.pma.climate.api.observation
package com.wanghaibo.vgashow.api.observation;


import android.os.Handler
import android.os.Looper
import com.ecarx.xui.adaptapi.car.base.ICarFunction.IFunctionValueWatcher
//import com.geely.pma.climate.api.ICarFunctionOperation
//import com.geely.pma.climate.api.SimpleIFunctionValueWatcher
import com.wanghaibo.vgashow.api.ICarFunctionOperation
import com.wanghaibo.vgashow.api.SimpleIFunctionValueWatcher
import java.lang.ref.WeakReference
import java.util.*

/**
 * Name: CarFunctionObservation
 *
 * Created by Wuhao on 2019/12/20
 *
 * Version: 0.0.1
 */
abstract class CarFunctionObservation internal constructor(
    // the function is null when observing all functions
    protected val function: Int?, protected val zones: IntArray?,
    val callerWatcher: IFunctionValueWatcher,
    protected val subscription: ICarFunctionSubscription
) : ICarFunctionOperation, SimpleIFunctionValueWatcher() {

    private val callLooper: WeakReference<Looper?> = WeakReference(Looper.myLooper())
//    private val callLooper: WeakReference<Looper> = WeakReference(Looper.myLooper())

    protected fun getCallLooper(): Looper? {
        return callLooper.get()
    }

    protected fun contains(array: IntArray, target: Int): Boolean {
        if (array.isEmpty()) {
            return false
        }
        for (item in array) {
            if (item == target) {
                return true
            }
        }
        return false
    }

    override fun onInitFailed(throwable: Throwable) {
        // do nothing
    }

    companion object {
        @JvmStatic
        protected val LOOPER_HANDLER_MAP: MutableMap<Looper, Handler> = WeakHashMap()
    }

}